//
//  AgentApi.m
//  IVCS
//
//  Created by nfl on 2022/2/28.
//  Copyright © 2022 zco user. All rights reserved.
//

#import "AgentApi.h"
@interface AgentApi(){
    ZealcommNetWorkTool *_tool;
}
@end
@implementation AgentApi

relizeSingleInstanceWithBlock(Agent, ^{
    self->_tool = [ZealcommNetWorkTool sharedNetWorkTool];
})

-(void)loginWithUrl:(NSString *)url name:(NSString *)name password:(NSString *)pwd org:(NSString *)org complete:(completeBlock)complete{
    CallBackObject *callBackResult =[[CallBackObject alloc] init];
    NSDictionary *para = @{
        @"userName":name,
        @"pwd":pwd,
        @"role":@"agent",
        @"org":org
    };
    NSString *apiUrl = [NSString stringWithFormat:@"%@/ivcs/api/v1/auth/logon",url];
    [_tool POST:apiUrl parameters:para HeaderKeyValues:nil success:^(id  _Nonnull responseObject) {
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        callBackResult.userInfo = result;
        callBackResult.code = 200;
        complete(callBackResult);
    } failure:^(NSError * _Nonnull error) {
        callBackResult.code = 101;//登录失败
        callBackResult.message = error.description;
        callBackResult.userInfo = error.userInfo;
        complete(callBackResult);
    }];
}

- (void)getBusinessScenariosByAgentName:(NSString *)agentName onSuccess:(void (^)(ZcoModelScenariosEnable * _Nonnull))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    NSString *apiUrl = [NSString stringWithFormat:@"%@/ivcs/api/v1/bizscenarios/agent/%@",self.baseUrl , agentName];
    [_tool GET:apiUrl parameters:@{} HeaderKeyValues:[self defaultHeaderKeyValues] success:^(id  _Nonnull responseObject) {
        NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        ZcoModelScenariosEnable *enable = [[ZcoModelScenariosEnable alloc] initWithDictionary:temp];
        if (onSuccess) {
            onSuccess(enable);
        }
    } failure:^(NSError * _Nonnull error) {
        if (onFailure) {
            onFailure(error);
        }
    }];
}

//FIXME: 返回数据没有全部获取
- (void)getBusinessScenarios:(void (^)(NSMutableArray<ZcoModelScenarioBrief *> * _Nonnull))onSuccess onFailure:(void (^)(NSError * _Nonnull))onFailure{
    NSString *apiUrl = [NSString stringWithFormat:@"%@/ivcs/api/v1/user/groups",self.baseUrl];
    [_tool GET:apiUrl parameters:@{} HeaderKeyValues:[self defaultHeaderKeyValues] success:^(id  _Nonnull responseObject) {
        NSArray *temp = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
        NSMutableArray<ZcoModelScenarioBrief *> *result = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in temp) {
            [result addObject:[[ZcoModelScenarioBrief alloc] initWithDictionary:dic]];
        }
        if (onSuccess) {
            onSuccess(result);
        }
    } failure:^(NSError * _Nonnull error) {
        if (onFailure) {
            onFailure(error);
        }
    }];
}


-(NSDictionary *)defaultHeaderKeyValues{
    return @{
        @"x-access-token":self.token
    };
}

@end
